<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
      <el-form-item label="模具编码" prop="accountId">
        <el-select @change="handleQuery" clearable class="!w-240px" v-model="queryParams.accountId" placeholder="请选择模具"
          check-strictly filterable>
          <el-option v-for="dict in accountList" :key="dict.id" :label="dict.code" :value="dict.id" />
        </el-select>
      </el-form-item>

      <el-form-item label="图号" prop="drawingNoId">
        <el-select @change="handleQuery" clearable class="!w-240px" v-model="queryParams.drawingNoId"
          placeholder="请选择图号" check-strictly filterable>
          <el-option v-for="dict in drawingNoList" :key="dict.id" :label="dict.drawingNo" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="保养类型" prop="upkeepId">
        <el-select @change="handleQuery" clearable class="!w-240px" v-model="queryParams.upkeepId" placeholder="请选择保养类型"
          check-strictly filterable>
          <el-option v-for="dict in upkeepCategoryList" :key="dict.id" :label="dict.name" :value="dict.id" />
        </el-select>
      </el-form-item>


      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>

        <el-button type="success" plain @click="handleExport" :loading="exportLoading"
          v-hasPermi="['mold:upkeep-statics:export']">
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>

      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table row-key="id" v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true"
      @selection-change="handleRowCheckboxChange">
      <el-table-column type="selection" width="55" />
      <!--       <el-table-column label="主键" align="center" prop="id" /> -->
      <!--       <el-table-column label="模具台账id" align="center" prop="accountId" /> -->
      <el-table-column label="模具名" align="center" prop="accountName" />
      <el-table-column label="模具编码" align="center" prop="accountCode" />
      <el-table-column label="图号" align="center" prop="drawingNo" />
      <el-table-column label="保养类型" align="center" prop="upkeepCategory" />
      <el-table-column label="保养次数" align="center" prop="upkeepCounts" />
      <el-table-column label="保养工时" align="center" prop="upkeepTotalTime" />
      <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" width="180px" />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button link type="primary" @click="queryListByAccountId(scope.row.accountId)">
            查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <UpkeepStaticsForm ref="formRef" @success="getList" />

  <!-- 保养任务列表弹窗 -->
  <UpkeepTaskList ref="taskListRef" />
</template>

<script setup lang="ts">
import { isEmpty } from '@/utils/is'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { UpkeepStaticsApi, UpkeepStatics } from '@/api/mold/upkeepstatics'
import UpkeepStaticsForm from './UpkeepStaticsForm.vue'
import UpkeepTaskList from './UpkeepTaskList.vue'
import { AccountApi } from '@/api/mold/account'
import { DrawingNoApi } from '@/api/mold/drawingno'
import { UpkeepCategoryApi } from '@/api/mold/upkeepcategory'
import { Console } from 'console'

/** 保养统计 列表 */
defineOptions({ name: 'UpkeepStatics' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<UpkeepStatics[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数

const accountList = ref([])
const drawingNoList = ref([])
const upkeepCategoryList = ref([])

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  accountId: undefined,
  drawingNoId: undefined,
  upkeepId: undefined,
  upkeepCounts: undefined,
  upkeepTotalTime: [],
  status: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const taskListRef = ref() // 保养任务列表弹窗引用

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await UpkeepStaticsApi.getUpkeepStaticsPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()


/** 查看按钮操作 */
const queryListByAccountId = async (id: number) => {
  taskListRef.value.open(id)
}

/** 批量删除保养统计 */
const handleDeleteBatch = async () => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    await UpkeepStaticsApi.deleteUpkeepStaticsList(checkedIds.value);
    message.success(t('common.delSuccess'))
    await getList();
  } catch { }
}

const checkedIds = ref<number[]>([])
const handleRowCheckboxChange = (records: UpkeepStatics[]) => {
  checkedIds.value = records.map((item) => item.id);
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await UpkeepStaticsApi.exportUpkeepStatics(queryParams)
    download.excel(data, '保养统计.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

const getAccountList = async () => {
  accountList.value = []
  const data = await AccountApi.getAccountAll()
  accountList.value = data
}


/** 获得图号列表 */
const getDrawingNoList = async () => {
  drawingNoList.value = []
  const data = await DrawingNoApi.getDrawingNoAll()
  drawingNoList.value = data
}

const getUpkeepCategoryList = async () => {
  upkeepCategoryList.value = []
  const data = await UpkeepCategoryApi.getUpkeepCategoryAll()
  upkeepCategoryList.value = data
}

/** 初始化 **/
onMounted(() => {
  getAccountList()
  getDrawingNoList()
  getUpkeepCategoryList()
  getList()
})
</script>